package book.test01;

import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

import java.io.IOException;

/**
 * 任务要求；分析不同城市的平均最小、大工资
 * 输出字段：城市名、平均最小工资，平均最大工资
 */
public class SalaryMapper extends Mapper<LongWritable, Text,Text,Text> {
    Text k = new Text();
    Text v = new Text();
    @Override
    protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
        //以“,”为分割符，分割数据
        //这里使用正则是因为分割的字段里面有些包含了分割符
        String[] data = value.toString().split(",(?=(?:[^\"]*\"[^\"]*\")*[^\"]*$)", -1);
        //找出所需要的字段
        String cityName = data[5];
        //只要工资前面的数字，将后面的单位k去掉
        String minSalary = data[2];
        String maxSalary = data[3];
        k.set(cityName);
        //将最大和最小工资一起设为v
        v.set(minSalary+","+maxSalary);
        //写出数据
        context.write(k,v);
    }
}
